//
//  LoginViewController.swift
//  约健
//
//  Created by tyh on 2017/9/26.
//  Copyright © 2017年 tyh. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController {


    @IBAction func IsSee(_ sender: UISwitch) {
         //self.password.isSecureTextEntry = (sender as UISwitch).isOn
    }
    @IBOutlet weak var tele: UITextField!
    @IBOutlet weak var password: UITextField!
    @IBAction func Login(_ sender: UIButton) {
        
        if tele.text == ""{
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(self.view, message: "未输入电话号码！！")
        }else if password.text == ""{
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(self.view, message: "未输入密码！！")
        }else{
        
        let parameters:NSDictionary = ["tel": tele.text!, "password": password.text!]
        print(parameters)
        NetWorkMngTool.sharedNetworkMngtool().RONetwork_Login(parameters, view: self.view, block: {
            (flag) in if flag == "1"
            {
              let _ = (ACSimpleKeychain.defaultKeychain() as AnyObject).storeUsername(self.tele.text, password: self.password.text, identifier: "user1", forService: "userpassWord")
                
                UIApplication.shared.keyWindow!.rootViewController = MainTabBarViewController()
            }
            else{
                
                MBProgressHUD.hide(for: self.view, animated: true)
                let _ = (ACSimpleKeychain.defaultKeychain() as AnyObject).deleteAllCredentials(forService: "userpassWord")
                let _ = (ACSimpleKeychain.defaultKeychain() as AnyObject).storeUsername(self.tele.text, password: "", identifier: "user1", forService: "userpassWord")
                MBProgressHUD.showDelayHUDToView(self.view, message: "账号或密码错误！！")
            }
        })
        
        }
        
    }
    @IBAction func Register(_ sender: UIButton) {
       self.performSegue(withIdentifier: "ToRegister", sender: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let bjimage = UIImageView(image: UIImage(named:"Login.jpg"))
        bjimage.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)
        
        self.view.addSubview(bjimage)
        self.view.sendSubview(toBack: bjimage)

      
        self.tele.borderStyle = .bezel
        self.tele.placeholder = "输入电话号码"
        
        self.password.borderStyle = .bezel
        self.password.placeholder = "输入密码"
        
          self.fetchUserNamePwd()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func fetchUserNamePwd()
    {
        let userPwdDict = (ACSimpleKeychain.defaultKeychain() as AnyObject).credentials(forIdentifier: "user1", service: "userpassWord") as NSDictionary
        if userPwdDict.count != 0 {
            tele.text = userPwdDict.object(forKey: "username") as? String
            password.text = userPwdDict["password"] as? String
        }
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.tele.resignFirstResponder()
        self.password.resignFirstResponder()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
